package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.mapper.*;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrMapper attrMapper;
    @Autowired
    SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;


    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    //通过catid查询该类下所有的属性信息
    @Override
    public List<AttrGroupEntity> queryAttrGourpByCatid(long catId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",catId);
        List<AttrGroupEntity> list = this.list(queryWrapper);
        list.forEach(t->{
            t.setAttrEntities(this.attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", t.getId()).eq("type",1)));
        });
        return list;
    }

    @Override
    public List<ItemGroupVo> queryAttrGroupBySpuIdAndCidAndSkuId(Long cid, Long skuId, Long spuId) {
        //查询所有组的id和值，设置组的id和值
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id", cid));
            List<ItemGroupVo> itemGroupVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(attrGroupEntities)) {
            attrGroupEntities.forEach(attrGroupEntity -> {
                ItemGroupVo itemGroupVo = new ItemGroupVo();
                itemGroupVo.setGroupId(attrGroupEntity.getId().intValue());
                itemGroupVo.setGroupName(attrGroupEntity.getName());

                List<AttrEntity> attrEntityList = this.attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", attrGroupEntity.getId()));
                //获取attr的id集合
                if (!CollectionUtils.isEmpty(attrEntityList)) {

                List<Long> attrIds = attrEntityList.stream().map(AttrEntity::getId).collect(Collectors.toList());

                //分别从spu和sku中查询参数，设置到item group的list属性中
                List<AttrValueVo> attrValueVos = new ArrayList<>();
                List<SpuAttrValueEntity> spuAttrValueEntityList = this.spuAttrValueMapper.selectList(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id", spuId).in("attr_id", attrIds));
                if (!CollectionUtils.isEmpty(spuAttrValueEntityList)) {
                    attrValueVos.addAll(spuAttrValueEntityList.stream().map(spuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        BeanUtils.copyProperties(spuAttrValueEntity, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }

                //从sku中查询所有的主题参数
                List<SkuAttrValueEntity> skuAttrValueEntities = this.skuAttrValueMapper.selectList(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in("attr_id", attrIds));
                if (!CollectionUtils.isEmpty(skuAttrValueEntities)) {
                    attrValueVos.addAll(skuAttrValueEntities.stream().map(skuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        BeanUtils.copyProperties(skuAttrValueEntity, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }
                    itemGroupVo.setAttrValues(attrValueVos);
                }
                    itemGroupVoList.add(itemGroupVo);
            });

        }

        return itemGroupVoList;
    }

}